package com.probox.modules.controller.order;

import cn.hutool.core.util.ObjectUtil;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.NumberUtil;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.core.utils.money.MoneyUtils;
import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.entity.domain.platfom.PlatformStoreGroup;
import com.probox.common.entity.service.platfom.IPlatformStoreGroupService;
import com.probox.common.entity.service.system.ISysUserService;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.common.entity.domain.order.OrderRecharge;
import com.probox.common.entity.service.order.IOrderRechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 充值订单Controller
 *
 * @author hds
 * @date 2021-06-07
 */
@RestController
@RequestMapping("/order/recharge")
public class OrderRechargeController extends BaseController {
    @Autowired
    private IOrderRechargeService orderRechargeService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IPlatformStoreGroupService storeGroupService;

    /**
     * 查询充值订单列表
     */
    @PreAuthorize(hasPermi = "order:recharge:list")
    @GetMapping("/list")
    public TableDataInfo list(OrderRecharge orderRecharge) {

        startPage();
        List<OrderRecharge> list = orderRechargeService.queryList(orderRecharge);
        return getDataTable(list);
    }

    /**
     * 统计储值
      * @param orderRecharge
     * @return
     */
    @PreAuthorize(hasPermi = "order:recharge:list")
    @GetMapping("/sumList")
    public AjaxResult sumList(OrderRecharge orderRecharge) {
        /*
        if(ObjectUtil.isEmpty(orderRecharge.getStoreId())){
            orderRecharge.setStoreId(SecurityUtils.getStoreId());
        }else{
            List<Long> stores = sysUserService.getUserStores(SecurityUtils.getUserId());
            if (stores != null) {
                if (orderRecharge.getStoreIds() == null) {
                    orderRecharge.setStoreIds(stores);
                } else {
                    orderRecharge.getStoreIds().addAll(stores);
                }
            }
        }
        if (orderRecharge.getStoreId() != null){
            if (orderRecharge.getStoreIds() == null){
                orderRecharge.setStoreIds(new ArrayList<>());
            }
            orderRecharge.getStoreIds().add(orderRecharge.getStoreId());
        }
        if (orderRecharge.getStoreIds() == null || orderRecharge.getStoreIds().size()==0){
            if (orderRecharge.getStoreGroupId() != null){
                PlatformStoreGroup group = storeGroupService.getById(orderRecharge.getStoreGroupId());
                if (group != null && StringUtils.isNotEmpty(group.getStoreIds())){
                    List<Long> ids = new ArrayList<>();
                    String[] storeIds = group.getStoreIds().split(",");
                    for(String s:storeIds){
                        if (StringUtils.isNotEmpty(s)){
                            ids.add(Long.parseLong(s));
                        }
                    }
                    if (ids.size()>0){
                        orderRecharge.setStoreIds(ids);
                    }
                }
            }
        }

        OrderRecharge rst = orderRechargeService.sumQueryList(orderRecharge);
        if (rst == null || rst.getPayAmount() == null){
            return AjaxResult.success("0");
        }else{
            return AjaxResult.success(MoneyUtils.changeF2Y(rst.getPayAmount()));
        }*/
        return AjaxResult.success("0");
    }

    /**
     * 导出充值订单列表
     */
    @PreAuthorize(hasPermi = "order:recharge:export")
    @Log(title = "充值订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderRecharge orderRecharge) throws IOException {

        List<OrderRecharge> list = orderRechargeService.queryList(orderRecharge);
        for(OrderRecharge or:list){
            or.setOrderAmountFmt(cn.hutool.core.util.NumberUtil.decimalFormat("##.##",or.getOrderAmount()/100.00));
            or.setPayAmountFmt(cn.hutool.core.util.NumberUtil.decimalFormat("##.##",or.getPayAmount()/100.00));
        }
        ExcelUtil<OrderRecharge> util = new ExcelUtil<OrderRecharge>(OrderRecharge.class);
        util.exportExcel(response, list, "充值订单数据");
    }

    /**
     * 获取充值订单详细信息
     */
    @PreAuthorize(hasPermi = "order:recharge:query")
    @GetMapping(value = "/{rechargeUid}")
    public AjaxResult getInfo(@PathVariable("rechargeUid") Long rechargeUid) {
        return AjaxResult.success(orderRechargeService.selectOrderRechargeById(rechargeUid));
    }

    /**
     * 新增充值订单
     */
    @PreAuthorize(hasPermi = "order:recharge:add")
    @Log(title = "充值订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderRecharge orderRecharge) {
        return toAjax(orderRechargeService.insertOrderRecharge(orderRecharge));
    }

    /**
     * 修改充值订单
     */
    @PreAuthorize(hasPermi = "order:recharge:edit")
    @Log(title = "充值订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderRecharge orderRecharge) {
        return toAjax(orderRechargeService.updateOrderRecharge(orderRecharge));
    }

    /**
     * 删除充值订单
     */
    @PreAuthorize(hasPermi = "order:recharge:remove")
    @Log(title = "充值订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{rechargeUids}")
    public AjaxResult remove(@PathVariable Long[] rechargeUids) {
        return toAjax(orderRechargeService.deleteOrderRechargeByIds(rechargeUids));
    }

    /**
     * 删除充值订单
     */
    @PreAuthorize(hasPermi = "order:recharge:recharge")
    @Log(title = "充值", businessType = BusinessType.DELETE)
    @PostMapping("/recharge")
    public AjaxResult recharge(@RequestBody OrderRecharge orderRecharge) {
        if (orderRecharge == null){
            throw new BaseException("参数不正确");
        }
        if (orderRecharge.getStoreId() == null){
            throw new BaseException("缺少储值门店");
        }
        if (orderRecharge.getCustomerUid() == null){
            throw new BaseException("缺少储值客户");
        }
        if (orderRecharge.getRechargeId() == null){
            throw new BaseException("缺少储值项目");
        }
        return toAjax(orderRechargeService.recharge(orderRecharge));
    }
}
